QuickOPC User's Guide and Reference
Installed Examples - Console - XmlEventLogger

The example below logs OPC Alarms and Events notifications into an XML file.

The main program:

// Logs OPC Alarms and Events notifications into an XML file.

using System;
using System.Diagnostics;
using System.Xml;
using System.Xml.Serialization;
using OpcLabs.BaseLib.Runtime.InteropServices;
using OpcLabs.EasyOpc.AlarmsAndEvents;
using OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel;

namespace XmlEventLogger
{
    class Program
    {
        static void Main()
        {
            ComManagement.Instance.AssureSecurityInitialization();

            Console.WriteLine("Starting up...");
            var xmlSerializer = new XmlSerializer(typeof(EasyAENotificationEventArgs));
            var xmlWriter = XmlWriter.Create("OpcEvents.xml", new XmlWriterSettings
            {
                Indent = true,
                CloseOutput = true
            });
            // The root element can have any name you need, but the name below also allows reading the log back as .NET array
            xmlWriter.WriteStartElement("ArrayOfEasyAENotificationEventArgs");

            Console.WriteLine("Logging events for 30 seconds...");
            int handle = EasyAEClient.SharedInstance.SubscribeEvents("", "OPCLabs.KitEventServer.2", 100,
               (_, eventArgs) =>
                   {
                       Debug.Assert(!(eventArgs is null));
                       Console.Write(".");
                       xmlSerializer.Serialize(xmlWriter, eventArgs);
                   });
            System.Threading.Thread.Sleep(30 * 1000);

            Console.WriteLine();
            Console.WriteLine("Shutting down...");
            EasyAEClient.SharedInstance.UnsubscribeEvents(handle);
            xmlWriter.WriteEndElement();    // not really necessary - XmlWriter would write the end tag for us anyway
            xmlWriter.Close();

            Console.WriteLine("Finished.");
        }
    }
}
' Logs OPC Alarms and Events notifications into an XML file.

Imports System.Xml
Imports System.Xml.Serialization
Imports OpcLabs.BaseLib.Runtime.InteropServices
Imports OpcLabs.EasyOpc.AlarmsAndEvents
Imports OpcLabs.EasyOpc.AlarmsAndEvents.OperationModel


Friend Class Program
    Shared WithEvents _client As New EasyAEClient
    Shared ReadOnly XmlSerializer As New XmlSerializer(GetType(EasyAENotificationEventArgs))
    Shared _xmlWriter As XmlWriter

    <MTAThread> ' needed for COM security initialization to succeed
    Shared Sub Main()
        ComManagement.Instance.AssureSecurityInitialization()

        Console.WriteLine("Starting up...")
        _xmlWriter = XmlWriter.Create("OpcEvents.xml", New XmlWriterSettings With {.Indent = True, .CloseOutput = True})
        ' The root element can have any name you need, but the name below also allows reading the log back as .NET array
        _xmlWriter.WriteStartElement("ArrayOfEasyAENotificationEventArgs")

        Console.WriteLine("Logging events for 30 seconds...")
        Dim handle As Integer = _client.SubscribeEvents("", "OPCLabs.KitEventServer.2", 100)
        Threading.Thread.Sleep(30 * 1000)

        Console.WriteLine()
        Console.WriteLine("Shutting down...")
        _client.UnsubscribeEvents(handle)
        _xmlWriter.WriteEndElement() ' not really necessary - XmlWriter would write the end tag for us anyway
        _xmlWriter.Close()

        Console.WriteLine("Finished.")
    End Sub

    Private Shared Sub Notification(ByVal sender As Object, ByVal eventArgs As Object) Handles _client.Notification
        Debug.Assert(eventArgs IsNot Nothing)
        Console.Write(".")
        XmlSerializer.Serialize(_xmlWriter, eventArgs)
    End Sub
End Class

 

See Also

Conceptual

Examples - OPC Alarms&Events